package com.dji.GSDemo.GaodeMap;

import dji.common.battery.BatteryState;
import dji.common.flightcontroller.FlightControllerState;
import dji.sdk.base.BaseProduct;
import dji.sdk.battery.Battery;
import dji.sdk.flightcontroller.FlightController;
import dji.sdk.products.Aircraft;

public class FlightStateMonitor {

    private double droneLocationLat = 181, droneLocationLng = 181, droneLocationAltitude = -1;
    private int remainingChargeInPercent = -1;

    private FlightController mFlightController;
    private Battery mBattery;

    private final FlightStateListener listener;

    public FlightStateMonitor(FlightStateListener flightStateListener) {
        listener = flightStateListener;
    }

    public interface FlightStateListener {
        void onFlightStateChanged(Double lat, Double lon, Double altitude);

        void onRemainingBatteryChanged(Integer remainingChargeInPercent);
    }

    public double getLat() {
        return droneLocationLat;
    }

    public double getLon() {
        return droneLocationLng;
    }

    public double getAltitude() {
        return droneLocationAltitude;
    }

    public int getRemainingChargeInPercent() {
        return remainingChargeInPercent;
    }

    // modify to get more info
    public void initFlightController() {
        BaseProduct product = DJIDemoApplication.getProductInstance();
        if (product != null && product.isConnected()) {
            if (product instanceof Aircraft) {
                mFlightController = ((Aircraft) product).getFlightController();
                mBattery = ((Aircraft) product).getBattery();
            }
        }

        if (mFlightController != null) {
            mFlightController.setStateCallback(
                    new FlightControllerState.Callback() {
                        @Override
                        public void onUpdate(FlightControllerState
                                                     djiFlightControllerCurrentState) {
                            updateFlightControllerStateInfo(djiFlightControllerCurrentState);
                        }
                    });
        }
        if (mBattery != null) {
            mBattery.setStateCallback(
                    new BatteryState.Callback() {
                        @Override
                        public void onUpdate(BatteryState batteryState) {
                            updateBatteryStateInfo(batteryState);
                        }
                    }
            );
        }
    }

    private void updateFlightControllerStateInfo(FlightControllerState
                                                         djiFlightControllerCurrentState) {
        if (Double.isNaN(djiFlightControllerCurrentState.getAircraftLocation().getLatitude()))
            return;
        droneLocationLat = djiFlightControllerCurrentState.getAircraftLocation().getLatitude();
        droneLocationLng = djiFlightControllerCurrentState.getAircraftLocation().getLongitude();
        droneLocationAltitude = djiFlightControllerCurrentState.getAircraftLocation().getAltitude();

        if (listener != null)
            listener.onFlightStateChanged(droneLocationLat, droneLocationLng, droneLocationAltitude);
    }

    private void updateBatteryStateInfo(BatteryState batteryState) {
        remainingChargeInPercent = batteryState.getChargeRemainingInPercent();

        if (listener != null)
            listener.onRemainingBatteryChanged(remainingChargeInPercent);
    }
}
